<script setup>
import { ref, onMounted, computed } from 'vue'
import { ElMessage } from 'element-plus'
import VoiceControlInterface from './components/VoiceControlInterface.vue'
import PetDragBar from './components/PetDragBar.vue'
import { Setting, Microphone, ChatDotRound, Monitor, Connection } from '@element-plus/icons-vue'

// 响应式数据
const showAdvancedMode = ref(false)
const isInitializing = ref(true)

// 检测是否在 Electron 环境中运行
const isElectron = computed(() => {
  return window.electronAPI && window.electronAPI.isElectron
})

// 生命周期
onMounted(async () => {
  try {
    // 检查后端服务连接
    const response = await fetch('http://localhost:18080/check')
    if (response.ok) {
      ElMessage.success('语音控制系统初始化成功！')
    } else {
      ElMessage.warning('后端服务连接失败，请检查服务是否启动')
    }
  } catch (error) {
    ElMessage.error('后端服务连接失败，请检查服务是否启动')
    console.error('初始化错误:', error)
  } finally {
    isInitializing.value = false
  }
})

// 方法
const toggleAdvancedMode = () => {
  showAdvancedMode.value = !showAdvancedMode.value
}
</script>

<template>
  <div id="app" :class="{ 'electron-mode': isElectron }">
    <!-- Electron 桌宠拖动条 -->
    <PetDragBar v-if="isElectron" />
    
    <!-- 加载状态 -->
    <div v-if="isInitializing" class="loading-screen" v-loading="true" element-loading-text="正在初始化语音系统...">
    </div>

    <!-- 简洁模式（默认） -->
    <div v-else-if="!showAdvancedMode" class="simple-mode" :class="{ 'has-drag-bar': isElectron }">
      <VoiceControlInterface />
    </div>

    <!-- 高级模式 -->
    <div v-else class="advanced-mode">
      <!-- 应用头部 -->
      <header class="app-header">
        <div class="header-content">
          <div class="logo-section">
            <el-icon class="app-icon" size="32">
              <Microphone />
            </el-icon>
            <h1 class="app-title">智能语音控制系统</h1>
          </div>
          
          <div class="header-actions">
            <el-tag 
              :type="systemStore.backendConnected ? 'success' : 'danger'"
              :effect="systemStore.backendConnected ? 'dark' : 'plain'"
              class="connection-tag"
            >
              {{ systemStore.backendConnected ? '已连接' : '未连接' }}
            </el-tag>
            
            <el-button
              type="primary"
              size="small"
              @click="toggleAdvancedMode"
              plain
            >
              简洁模式
            </el-button>
          </div>
        </div>
      </header>

      <!-- 主要内容区域 -->
      <main class="app-main">
        <div class="main-container">
          <div class="content-area">
            <!-- 标签页导航 -->
            <el-tabs v-model="activeTab" class="main-tabs" type="border-card">
              <el-tab-pane label="语音控制" name="voice">
                <template #label>
                  <span class="tab-label">
                    <el-icon><Microphone /></el-icon>
                    语音控制
                  </span>
                </template>
                <VoiceControlPanel />
              </el-tab-pane>
              
              <el-tab-pane label="智能指令" name="command">
                <template #label>
                  <span class="tab-label">
                    <el-icon><ChatDotRound /></el-icon>
                    智能指令
                  </span>
                </template>
                <CommandInterface />
              </el-tab-pane>
              
              <el-tab-pane label="系统状态" name="status">
                <template #label>
                  <span class="tab-label">
                    <el-icon><Monitor /></el-icon>
                    系统状态
                  </span>
                </template>
                <SystemStatus />
              </el-tab-pane>
              
              <el-tab-pane label="实时监控" name="realtime">
                <template #label>
                  <span class="tab-label">
                    <el-icon><Connection /></el-icon>
                    实时监控
                  </span>
                </template>
                <RealtimeStatus />
              </el-tab-pane>
            </el-tabs>
          </div>
        </div>
      </main>

      <!-- 应用底部 -->
      <footer class="app-footer">
        <div class="footer-content">
          <p>&copy; 2024 智能语音控制系统 - 基于大模型的语音交互解决方案</p>
          <div class="footer-links">
            <el-link href="#" type="primary">使用文档</el-link>
            <el-link href="#" type="primary">API接口</el-link>
            <el-link href="#" type="primary">技术支持</el-link>
          </div>
        </div>
      </footer>
    </div>
  </div>
</template>

<style scoped>
#app {
  width: 100vw;
  height: 100vh;
  margin: 0;
  padding: 0;
  overflow: hidden;
  position: fixed;
  top: 0;
  left: 0;
}

/* Electron 桌宠模式样式 */
#app.electron-mode {
  background: transparent;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3);
}

/* 加载屏幕 */
.loading-screen {
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

/* 简洁模式 */
.simple-mode {
  width: 100vw;
  min-height: 100vh;
  position: relative;
}

/* 有拖动条时的布局调整 */
.simple-mode.has-drag-bar {
  padding-top: 40px;
  height: calc(100vh - 40px);
  min-height: auto;
}

/* 高级模式 */
.advanced-mode {
  width: 100vw;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.app-header {
  background: white;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  height: 70px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 16px;
}

.connection-tag {
  margin-right: 8px;
}

.logo-section {
  display: flex;
  align-items: center;
  gap: 12px;
}

.app-icon {
  color: #409eff;
}

.app-title {
  margin: 0;
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  background: linear-gradient(45deg, #409eff, #67c23a);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}


.app-main {
  flex: 1;
  padding: 20px;
}

.main-container {
  max-width: 1200px;
  margin: 0 auto;
}


.content-area {
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.main-tabs {
  border: none;
  box-shadow: none;
}

.tab-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 500;
}

.app-footer {
  background: #303133;
  color: white;
  padding: 20px;
  margin-top: auto;
}

.footer-content {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 20px;
}

.footer-content p {
  margin: 0;
  font-size: 14px;
  color: #c0c4cc;
}

.footer-links {
  display: flex;
  gap: 20px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-content {
    padding: 0 15px;
    height: 60px;
  }
  
  .app-title {
    font-size: 18px;
  }
  
  .app-main {
    padding: 15px;
  }
  
  .header-actions {
    flex-direction: column;
    gap: 8px;
  }
  
  .footer-content {
    flex-direction: column;
    text-align: center;
    gap: 15px;
  }
  
  .footer-links {
    justify-content: center;
  }
}

@media (max-width: 480px) {
  .header-content {
    height: 50px;
  }
  
  .app-title {
    font-size: 16px;
  }
}

/* Element Plus 样式覆盖 */
:deep(.el-tabs__header) {
  margin: 0;
  background: #f8f9fa;
}

:deep(.el-tabs__nav-wrap) {
  padding: 0 20px;
}

:deep(.el-tabs__content) {
  padding: 30px;
}

:deep(.el-tab-pane) {
  animation: fadeIn 0.3s ease-in-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 全局滚动条样式 */
:deep(::-webkit-scrollbar) {
  width: 8px;
  height: 8px;
}

:deep(::-webkit-scrollbar-track) {
  background: #f1f1f1;
  border-radius: 4px;
}

:deep(::-webkit-scrollbar-thumb) {
  background: #c1c1c1;
  border-radius: 4px;
}

:deep(::-webkit-scrollbar-thumb:hover) {
  background: #a8a8a8;
}
</style>
